import _data_collection as dc
import _data_save as ds
import _params

# 获取股票列表（基本信息）并保存至文件
def data_stock_list():
    if _params.__STOCK_LIST_API_TYPE == 0:
        stock_list = dc.collect_stock_list_by_tl('A')
        field = 'ticker'
    elif _params.__STOCK_LIST_API_TYPE == 1:
        stock_list = dc.collect_stock_list_by_sina()
        field = 'code'
    else:
        # 默认用通联
        stock_list = dc.collect_stock_day_by_tl('A')
        field = 'ticker'

    # 将数据保存至excel文件
    ds.save_stock_list_to_file_addition(data=stock_list, field=field, file_full_path=_params.__STOCK_LIST_FULL_FILE_PATH)
    print("获取股票列表完成！股票数量：" + str(len(stock_list)))

# 获取股票的行情数据，并保存至文件(单个文件保存单个股票的日线数据）
def data_stock_hq(tickers, beginDate='', endDate=''):
    # 循环处理每个股票的数据
    for ticker in tickers:
        print("开始处理数据：code=" + ticker)
        if _params.__STOCK_HQ_API_TYPE == 0:
            stock_hq = dc.collect_stock_day_by_tl(ticker=ticker,beginDate=beginDate, endDate=endDate)
            field = 'tradeDate'
        else:
            # 默认使用通联
            stock_hq = dc.collect_stock_day_by_tl(ticker=ticker,beginDate=beginDate, endDate=endDate)
            field = 'tradeDate'
        # 格式化数据
        stock_hq[field] = stock_hq[field].apply(lambda x: x.replace('-', ''))
        # 将行情数据保存至文件
        ds.save_stock_list_to_file_addition(stock_hq,field,_params.__STOCK_HQ_FILE_PATH + ticker + '.xlsx')
        print("--处理数据完成：code=" + ticker)
